package com.sj.purchase.modules.helper.web;

import com.sj.purchase.modules.helper.model.OutBoundUniqueCode;
import com.sj.purchase.modules.helper.repository.OutBoundUniqueCodeRepository;
import com.sj.purchase.util.Date2StrUtils;
import com.sj.purchase.util.ExcelDownloadUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.persistence.criteria.Predicate;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 微宝落地
 *
 * @author yangrd
 * @date 2018/8/16
 **/
@RestController
@RequestMapping("/api/out-bound-unique-code")
public class OutBoundUniqueCodeController {

    private OutBoundUniqueCodeRepository repository;

    @Autowired
    public OutBoundUniqueCodeController(OutBoundUniqueCodeRepository repository) {
        this.repository = repository;
    }

    @GetMapping
    public Page<OutBoundUniqueCodeVO> findAll(Date start, Date end, String shopName, String spuNos, String skuNos, Pageable pageable) {
        return repository.findAll(specification(start, end, shopName, spuNos, skuNos), pageable).map(OutBoundUniqueCodeAssembler::toVO);
    }

    @GetMapping("/excel")
    public void findAll(Date start, Date end, String shopName, String spuNos, String skuNos) {
        String excelName = String.format("%s--%s %s", format(start), format(end), "出库详情单");
        List<OutBoundUniqueCodeVO> list = repository.findAll(specification(start, end, shopName, spuNos, skuNos)).stream().map(OutBoundUniqueCodeAssembler::toVO).collect(Collectors.toList());
        ExcelDownloadUtil.easyPoi(excelName, OutBoundUniqueCodeVO.class, list);
    }

    private String format(Date date) {
        return Date2StrUtils.str(date).replaceAll("/", "-");
    }

    private Specification<OutBoundUniqueCode> specification(Date start, Date end, String shopName, String spuNos, String skuNos) {
        return (root, query, cb) -> {
            Predicate predicate = cb.conjunction();
            if (Objects.nonNull(start) && Objects.nonNull(end)) {
                predicate.getExpressions().add(cb.between(root.get("createDate").as(Date.class), start, end));
            }
            if (StringUtils.isNotBlank(shopName)) {
                predicate.getExpressions().add(cb.equal(root.get("shopName").as(String.class), shopName));
            }
            if (StringUtils.isNotBlank(spuNos)) {
                predicate.getExpressions().add(root.get("productNo").as(String.class).in(toList(spuNos)));
            }
            if (StringUtils.isNotBlank(skuNos)) {
                predicate.getExpressions().add(root.get("skuNo").as(String.class).in(toList(skuNos)));
            }
            return predicate;
        };
    }

    private List<String> toList(String nos) {
        List<String> noList = Collections.emptyList();
        if (StringUtils.isNotBlank(nos)) {
            String splitStr = ",";
            noList = Collections.singletonList(nos);
            if (nos.contains(splitStr)) {
                noList = Arrays.asList(nos.split(splitStr));
            }
        }
        return noList;
    }

}
